package com.amorcloud.pig.widget;

import android.content.Context;
import android.content.Intent;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.amorcloud.pig.R;
import com.amorcloud.pig.activity.AddUserActivity;
import com.amorcloud.pig.activity.PigListActivity;

public class CustomToolbar extends LinearLayout {

    TextView mBack, mTitle, mAddUser;
    ImageView mPigList;

    public CustomToolbar(Context context) {
        super(context);
    }

    public CustomToolbar(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    public CustomToolbar(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        mBack = findViewById(R.id.toolbar_back);
        mTitle = findViewById(R.id.toolbar_title);
        mAddUser = findViewById(R.id.tv_add_user);
        mAddUser.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getContext(), AddUserActivity.class);
                intent.putExtra("title", "新增用户");
                getContext().startActivity(intent);
            }
        });

        mPigList = findViewById(R.id.imv_pig_list);
        mPigList.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getContext(), PigListActivity.class);
                intent.putExtra("title", "带标信息");
                getContext().startActivity(intent);
            }
        });
    }

    public void setTitle(String title) {
        if(title != null) {
            mTitle.setText(title);
        }
    }

    public void setBackClick(OnClickListener listener) {
        if(listener != null) {
            mBack.setOnClickListener(listener);
        }
    }

    public void hideBack() {
        if(mBack != null) {
            mBack.setVisibility(View.INVISIBLE);
        }
    }

    public void showAddUser() {
        if(mAddUser != null) {
            mAddUser.setVisibility(View.VISIBLE);
        }
    }

    public void showPigList() {
        if(mPigList != null) {
            mPigList.setVisibility(View.VISIBLE);
        }
    }

}
